const { ListNode } = require('../../utils/list.js')

/**
 * @param {ListNode} head
 * @return {ListNode}
 */
var deleteDuplicates = function (head) {
  let cur = head
  while (cur && cur.next) {
    if (cur.val === cur.next.val) {
      cur.next = cur.next.next
    } else {
      cur = cur.next
    }
  }
  return head
};

/**
 * 快慢指针解法
 * @param {ListNode} head
 * @return {ListNode}
 */
var deleteDuplicates2 = function (head) {
  if (!head) return null;
  let slow = head;
  let fast = head;
  while (fast !== null) {
    // 快慢指针不等时
    if (slow.val !== fast.val) {
      slow.next = fast;
      slow = fast;
    }
    // 快指针前进一步
    fast = fast.next;
  }
  // 断尾，删除多余重复元素
  slow.next = null;
  return head;
};
